package sv.com.renacempleo.web.controller.ctg;
 
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import sv.com.renacempleo.dto.ctg.CtgEmpleados;
import sv.com.renacempleo.exception.WebException;
import sv.com.renacempleo.web.controller.BaseController;
import sv.com.renacempleo.web.util.DBContext;
import sv.com.renacempleo.web.util.WebUtilidades;
 
@Controller
@RequestMapping("/admin/empleado")
public class CtgEmpleadoController extends BaseController{
 
      private static final long serialVersionUID = -1400605959996167486L;
     
      @RequestMapping(value = "view", method = {RequestMethod.POST})
      public String view(ModelMap model,
                  @RequestParam (required = false) String header) {
            model.addAttribute(WebUtilidades.HEADER_TITLE, StringUtils.isBlank(header) ? "EMPLEADOS" : header);
            try {
                  model.put("ctgOficinas", JSONArray.fromObject(DBContext.getCtgOficinasDao().findAllActivesAsArray()));
                  model.put("ctgCargoEmpleados", JSONArray.fromObject(DBContext.getCtgCatalogoDao().findAllActivesByCodigoAsArray(WebUtilidades.CTG_CARGOS_EMPLEADO_PADRE)));
                  model.put("data", JSONArray.fromObject(DBContext.getCtgEmpleadoDao().findAllAsArray()));
            } catch (WebException e) {
                  logger.error("view", e);
            }
            return "admin/catalogo/ctgEmpleado";
      }
     
      @RequestMapping(value = "save", method = {RequestMethod.POST})
      public void save(
                  @ModelAttribute CtgEmpleados ctgEmpleados,
                  HttpServletRequest request,
                  HttpServletResponse response)
      {
            Map<String, Object> result = new HashMap<String, Object>();
            try {
                  SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
                  String fechaIngreso = String.valueOf(format.format(ctgEmpleados.getCtgEmpleadoFechaIngreso()));
                  String fechaRetiro = String.valueOf(format.format(ctgEmpleados.getCtgEmpleadoFechaRetiro()));
                  ctgEmpleados.setCtgEmpleadoFechaIngreso(fechaIngreso);
                  ctgEmpleados.setCtgEmpleadoFechaRetiro(fechaRetiro);
                 
                  DBContext.getCtgEmpleadoDao().save(ctgEmpleados);
                  result.put("data", JSONArray.fromObject(DBContext.getCtgEmpleadoDao().findAllAsArray()));
                  result.put(SUCCESS, true);
                  result.put(MESSAGE, UPDATED_DATA);
            } catch (WebException e) {
                  logger.error("save", e);
                  result.put(FAILURE, true);
                  result.put(MESSAGE, e.getExceptionMessage());
            } catch (Exception e) {
                  logger.error("save", e);
                  result.put(FAILURE, true);
                  result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
            }
            serializeObject(result, response);
      }
 
}